



#include	"main.h"










/********************************************************
**函数名：	BSW_LPSPI_INIT
**功能：		初始化 LPSPI_INIT
**输入参数	Lpsi_Cfg	SPI配置结构体
**返回值：	无
**注意：		根据相应的电路配置SPI
*********************************************************/
void BSW_LPSPI_INIT(Bsw_Lpspi_Cfg Lpsi_Cfg)
{
	PCC_SetClockMode(PCC, Lpsi_Cfg.clockName, false);                                                    /* 为给定的外设启用/禁用时钟 eg:禁止                                    	  */ 
	PCC_SetPeripheralClockControl(PCC, Lpsi_Cfg.clockName, true, Lpsi_Cfg.clockSource, 0, 0);            /* 设置SOSC控制寄存器                                                    	*/
	PCC_SetClockMode(PCC,Lpsi_Cfg.clockName, true);                                                      /* 为给定的外设启用/禁用时钟 eg:启用                                     	*/ 
	
	LPSPI_Disable(Lpsi_Cfg.base);																                                         /* 禁用LPSPI模块 eg:禁止                                                 	*/
	LPSPI_SetIntMode(Lpsi_Cfg.base,LPSPI_ALL_STATUS,false);	                                             /* 配置LPSPI中断 eg:禁止                                                 	*/
	LPSPI_SetTxDmaCmd(Lpsi_Cfg.base,Lpsi_Cfg.b_TxDmaCmd);							                            		   /* 设置LPSPI传输数据DMA配置（启用/禁止）TXDMA not used                  	 	*/
	LPSPI_SetRxDmaCmd(Lpsi_Cfg.base,Lpsi_Cfg.b_RxDmaCmd);									                               /* 设置LPSPI接收数据DMA配置（启用/禁止）RXDMA not used                   	*/
	LPSPI_SetMasterSlaveMode(Lpsi_Cfg.base, Lpsi_Cfg.Lpspi_mode);					                               /* 配置主从LPSPI  eg:Set for master mode                                 	*/ 
	LPSPI_SetPinConfigMode(Lpsi_Cfg.base, Lpsi_Cfg.PinCfg, Lpsi_Cfg.DataOutCfg, Lpsi_Cfg.b_Pcs_Enable);  /* 配置LPSPI/SPI Pin配置模式                                             	*/
	
	LPSPI_ClearStatusFlag(Lpsi_Cfg.base,LPSPI_ALL_STATUS);                                               /* 清除LPSPI状态标志位                                                   	*/
	LPSPI_SetBaudRateDivisor(Lpsi_Cfg.base,Lpsi_Cfg.u32_SCKDIV);								                         /* 手动配置波特率因子（只有LPSPI_CCR[SCKDIV]）SCKDIV=8: SCK divider =8+2 = 10 (1 usec: 1 MHz baud rate)        */
	LPSPI_SetDelay(Lpsi_Cfg.base, LPSPI_SCK_TO_PCS, Lpsi_Cfg.u32_SCKPCS);					                       /* 手动配置特定的LPSPI延迟参数(必须禁用模块以更改延迟值) eg:SCKPCS=4: SCK to PCS delay = 4+1 = 5 (500 nsec)    */
	LPSPI_SetDelay(Lpsi_Cfg.base, LPSPI_PCS_TO_SCK,Lpsi_Cfg.u32_PCSSCK);						                     /* 手动配置特定的LPSPI延迟参数(必须禁用模块以更改延迟值) eg:PCSSCK=9: PCS to SCK delay = 9+1 = 10 (1 usec)     */
	LPSPI_SetDelay(Lpsi_Cfg.base, LPSPI_BETWEEN_TRANSFER,Lpsi_Cfg.u32_DBT);					                     /* 手动配置特定的LPSPI延迟参数(必须禁用模块以更改延迟值) eg:DBT=8: Delay between Transfers = 8+2 = 10 (1 usec) */
	LPSPI_SetPcsPolarityMode(Lpsi_Cfg.base,Lpsi_Cfg.Lpspi_Pcs ,Lpsi_Cfg.PcsPolarity);                    /* 配置所需的LPSPI PCS极性                                               	*/
	
	LPSPI_SetSamplingPoint(Lpsi_Cfg.base,Lpsi_Cfg.b_SampleDly);                                          /* 如果主设备的样点延迟，则配置                                            */
	LPSPI_SetRxWatermarks(Lpsi_Cfg.base,Lpsi_Cfg.rxWater);									                             /* 设置RX FIFO watermask 值 eg:RXWATER=0: Rx flags set when Rx FIFO >0     */
	LPSPI_SetTxWatermarks(Lpsi_Cfg.base,Lpsi_Cfg.txWater);									                             /* 设置RX FIFO watermask 值 eg:TXWATER=3: Tx flags set when Tx FIFO <= 3   */
	LPSPI_SetTxCommandReg(Lpsi_Cfg.base,Lpsi_Cfg.Cmd_Cfg);                                               /* 设置传输命令寄存器TCR的参数                                             */
	
	LPSPI_Enable(Lpsi_Cfg.base);	
}





void BSW_LPSPI_WR_BIT(u8 *u8TxData,u8 *u8RxData, u8 u8BuffLen)  
{
	u8 u8_Count = 0;
	
	LPSPI0->CR |=LPSPI_CR_RRF(1);
	LPSPI0->TDR = u8TxData[0]; 
	for(u8_Count = 1;u8_Count < u8BuffLen;u8_Count++)
	{
		while(!LPSPI_GetStatusFlag(LPSPI0, LPSPI_WORD_COMPLETE));
		LPSPI0->TDR = u8TxData[u8_Count]; 
		LPSPI_ClearStatusFlag(LPSPI0,LPSPI_WORD_COMPLETE);
		
		while(!LPSPI_GetStatusFlag(LPSPI0,LPSPI_RX_DATA_FLAG));
		u8RxData[u8_Count-1] = LPSPI0->RDR; 
		LPSPI_ClearStatusFlag(LPSPI0,LPSPI_RX_DATA_FLAG);
	}
	while(!LPSPI_GetStatusFlag(LPSPI0, LPSPI_WORD_COMPLETE));
	LPSPI_ClearContCBit(LPSPI0);
	LPSPI_ClearStatusFlag(LPSPI0,LPSPI_WORD_COMPLETE);
	
	while(!LPSPI_GetStatusFlag(LPSPI0,LPSPI_RX_DATA_FLAG));
	u8RxData[u8_Count-1] = LPSPI0->RDR; 
	LPSPI_ClearStatusFlag(LPSPI0,LPSPI_RX_DATA_FLAG);
}


void BSW_LPSPI_SEND(u8 *u8TxData, u8 u8BuffLen)
{
	u8 u8_Count = 0;
	LPSPI0->TDR = u8TxData[0]; 
	for(u8_Count = 1;u8_Count < u8BuffLen;u8_Count++)
	{
		while(!LPSPI_GetStatusFlag(LPSPI0, LPSPI_WORD_COMPLETE));
		LPSPI0->TDR = u8TxData[u8_Count]; 
		LPSPI_ClearStatusFlag(LPSPI0,LPSPI_WORD_COMPLETE);
	}
	while(!LPSPI_GetStatusFlag(LPSPI0, LPSPI_WORD_COMPLETE));
	LPSPI_ClearContCBit(LPSPI0);
	LPSPI_ClearStatusFlag(LPSPI0,LPSPI_WORD_COMPLETE);
}



inline void BSW_LPSPI_START(LPSPI_Type* base,u16 u16TxData)
{
	base->CR |= LPSPI_CR_RRF(1);
	base->TDR = u16TxData; 
}



u16 BSW_LPSPI_WR(LPSPI_Type* base,u16 u16TxData) 
{
  u16 u16_RxData = 0;
	uint16_t u16_Count = 0;
	while(!LPSPI_GetStatusFlag(base, LPSPI_WORD_COMPLETE));           
	base->TDR = u16TxData;   
	while(!LPSPI_GetStatusFlag(base,LPSPI_RX_DATA_FLAG))
	{
		if(u16_Count > 300)
		{
			break;
		}
		u16_Count ++;
	}
	u16_RxData = base->RDR ;            /* Read received data */
	LPSPI_ClearStatusFlag(base,LPSPI_RX_DATA_FLAG);
	
  return u16_RxData;  
}


u16 BSW_LPSPI_END(LPSPI_Type* base)
{
	u8 u8_Count = 0;
	u16 u16_RxData = 0;
	while(!LPSPI_GetStatusFlag(base, LPSPI_TX_DATA_FLAG))
	{
		if(u8_Count > 200)
		{
			break;
		}
		u8_Count ++;
	}
	LPSPI_ClearContCBit(base);
	LPSPI_ClearStatusFlag(base,LPSPI_TX_DATA_FLAG);
	while(!LPSPI_GetStatusFlag(base,LPSPI_RX_DATA_FLAG));
	u16_RxData = base->RDR; 
	LPSPI_ClearStatusFlag(base,LPSPI_RX_DATA_FLAG);
	base->CR |=LPSPI_CR_RRF(1);
	return u16_RxData;
}





void SPITxSendBuffer(u8 *u8TxData, u8 *u8RxData,u8 u8TxLen)  
{
	u8 u8_Count = 0;
	BSW_LPSPI_START(LPSPI1,u8TxData[0]);
	for(u8_Count = 1;u8_Count<u8TxLen;u8_Count++)
	{
		u8RxData[u8_Count-1] = BSW_LPSPI_WR(LPSPI1,u8TxData[u8_Count]);
	}
	u8RxData[u8_Count-1] = BSW_LPSPI_END(LPSPI1);
}




u16 BSW_LPSPI_WR8BIT(LPSPI_Type * base,u16 u16TxData) 
{
	return 1;
}



uint16_t BSW_LPSPI_WR16BIT(LPSPI_Type * base,uint16_t Send_Value) 
{
  uint16_t DataValue = 0;
  while(!(LPSPI_GetStatusFlag(base,LPSPI_TX_DATA_FLAG)));		/* Wait for Tx FIFO available */
	LPSPI_WriteData(base,Send_Value);													/* Transmit data */
	DataValue= LPSPI_ReadData(base);            							/* Read received data */
	while(((LPSPI0->SR & LPSPI_SR_WCF_MASK)>>LPSPI_SR_WCF_SHIFT) ==0);  
	
  return DataValue;  
}




uint16_t BSW_READ_FIFO_SIZE(void)
{
	return ((LPSPI0->FSR & 0x0F0000)>> 8)+ (LPSPI0->FSR & 0x00000F) ;
}

u8 G_u8_Finish_Flg = false;
u8 G_u8_Lpspi_TxBuff[16]= {0};
u8 G_u8_Lpspi_RxBuff[16]= {0};
u8 G_u8_Lpspi_Count = 0;
u8 G_u8_Send_Lenth =0;

u8 BSW_LPSPI_SEND_ARRY(u8* P_u8_TxBuff,u8 u8_Lenth)
{
	u8 u8_Status = 0;
	u8 u8_TxCount = 0;
	G_u8_Finish_Flg = false;
	for(u8_TxCount = 0;u8_TxCount < u8_Lenth;u8_TxCount++)
	{
		G_u8_Lpspi_TxBuff[u8_TxCount] = P_u8_TxBuff[u8_TxCount];
	}
	LPSPI0->TDR = G_u8_Lpspi_TxBuff[0];
	G_u8_Send_Lenth = u8_Lenth;
	return u8_Status;
}

u8 BSW_LPSPI_Finish_Flg(void)
{
	return G_u8_Finish_Flg;
}

void BSW_LPSPI_REVE_ARRY(u8* P_u8_RxBuff,u8 u8_Lenth)
{
	u8 u8_RxCount = 0;
	for(u8_RxCount = 0; u8_RxCount < u8_Lenth;u8_RxCount ++)
	{
		P_u8_RxBuff[u8_RxCount] = G_u8_Lpspi_RxBuff[u8_RxCount];
	}
}




void LPSPI0_IRQHandler(void)
{
	if(LPSPI_GetStatusFlag(LPSPI0, LPSPI_RX_DATA_FLAG))
	{
		LPSPI_ClearStatusFlag(LPSPI0,LPSPI_RX_DATA_FLAG);
		BSW_GPIO_OPPOSITE(PTD,13);
		G_u8_Lpspi_RxBuff[G_u8_Lpspi_Count - 1] = LPSPI0->RDR;

		if(G_u8_Lpspi_Count >= G_u8_Send_Lenth)
		{
			G_u8_Finish_Flg = true;
			G_u8_Lpspi_Count = 0;
		}
	}
	if(LPSPI_GetStatusFlag(LPSPI0, LPSPI_WORD_COMPLETE))  /* LPSPI_TRANSMIT_ERROR LPSPI_WORD_COMPLETE LPSPI_FRAME_COMPLETE */
	{
		G_u8_Lpspi_Count ++;
		if(G_u8_Lpspi_Count < G_u8_Send_Lenth)
		{
			LPSPI0->TDR = G_u8_Lpspi_RxBuff[G_u8_Lpspi_Count];
		}
		else
		{
			LPSPI_ClearContCBit(LPSPI0);
		}
		LPSPI_ClearStatusFlag(LPSPI0,LPSPI_WORD_COMPLETE);
	}
	
}































